package ua.pp.bizon.yandex.api.impl;

import ua.pp.bizon.yandex.api.IJobExecutor;
import ua.pp.bizon.yandex.api.Job;
import ua.pp.bizon.yandex.api.JobInfo;
import ua.pp.bizon.yandex.api.ObjectLookup;
import ua.pp.bizon.yandex.api.WindowManager;
import ua.pp.bizon.yandex.nb.AddPhotosController;
import ua.pp.bizon.yandex.nb.AddPhotosDialogData;
import ua.pp.bizon.yandex.nb.ProgressPanel;
import ua.pp.bizon.yandex.uploader.atom.EntryAlbum;

import java.io.File;
import java.util.List;
import java.util.TreeMap;

import org.apache.commons.logging.LogFactory;

public class UploadFilesJob implements JobInfo {
	private final List<File> from;
	private final EntryAlbum to;
	private final IJobExecutor executor;

	public UploadFilesJob(List<File> from, EntryAlbum to, IJobExecutor executor) {
		this.from = from;
		this.to = to;
		this.executor = executor;
		LogFactory.getLog(getClass()).trace("new " + toString());
	}

	@Override
	public void run() {
		final ProgressPanel dialog = ObjectLookup.lookup(WindowManager.class).progressPanel();
		AddPhotosDialogData data = dialog.getData();
		data.setTotalcount(from.size());
		data.setTo(to.getName());
		executor.exec(new AddPhotosController(dialog, from, to));
		executor.execInSwing(new Job() {
			public void run() {
				dialog.setVisible(true);
			}
		});
	}

	@Override
	public Element getState() {
		Element element = new Element();
		element.setActivity(Activity.UPLOAD);
		element.setProperties(new TreeMap<String, String>());
		element.getProperties().put(Element.PROP_FROM, from.toString());
		element.getProperties().put(Element.PROP_TO, to.getID());
		element.getProperties().put(Element.PROP_COUNT_DONE, "0");
		element.getProperties().put(Element.PROP_COUNT_TOTAL, "" + from.size());
		return element;
	}
}